// // Copyright (c) 2009 All Right Reserved // // vl // // 2009-01-01 // Contains ... using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.Contracts; using System.Globalization; using System.Text; using System.Xml.Linq; using JetBrains.Annotations; using LargoCommon.Abstract; namespace LargoCommon.Music { /// Musical material. /// Musical class. //// [Serializable] [ContractVerification(false)] public sealed class RhythmicMaterial { #region Constructors /// Initializes a new instance of the RhythmicMaterial class. public RhythmicMaterial() { this.Structures = new List(); } /// /// Initializes a new instance of the class. /// /// The mark material. public RhythmicMaterial(XElement xmaterial) { this.Name = XmlSupport.ReadStringAttribute(xmaterial.Attribute("Name")); this.RhythmicOrder = XmlSupport.ReadByteAttribute(xmaterial.Attribute("RhythmicOrder")); this.Structures = new List(); var xstructs = xmaterial.Element("Structures"); if (xstructs == null) { return; } foreach (var xstruct in xstructs.Elements()) { var rs = RhythmicSystem.GetRhythmicSystem(RhythmicDegree.Structure, this.RhythmicOrder); var code = XmlSupport.ReadStringAttribute(xstruct.Attribute("Code")); var structure = new RhythmicStructure(rs, code); this.Structures.Add(structure); } } #endregion #region Properties - Xml /// Gets Xml representation. /// Property description. public XElement GetXElement { get { XElement xmaterial = new XElement( "RhythmicMaterial", new XAttribute("Name", this.Name), new XAttribute("RhythmicOrder", this.RhythmicOrder)); //// Structures XElement xstructs = new XElement("Structures"); foreach (RhythmicStructure structure in this.Structures) { var xstruct = structure.GetXElement; xstructs.Add(xstruct); } xmaterial.Add(xstructs); return xmaterial; } } #endregion #region Properties /// /// Gets or sets the name. /// /// /// The melodic order. /// [UsedImplicitly] public string Name { get; set; } //// CA1044 (FxCop) /// /// Gets or sets the rhythmic order. /// /// /// The rhythmic order. /// public byte RhythmicOrder { [UsedImplicitly] get; set; } /// /// Gets the structures. /// /// /// The structures. /// public IList Structures { get; } #endregion #region Rhythmic producer /// /// Random Rhythmic Materials. /// /// Number of material sets. /// Rhythmic Structs. /// Number Of Structs. /// /// Returns value. /// [UsedImplicitly] public static Collection RandomRhythmicMaterials(int number, Collection rhyStructs, int numberOfStructs) { //// byte rhythmicOrder, var coll = new Collection(); for (var i = 0; i < number; i++) { var name = string.Format(CultureInfo.CurrentCulture, "Automat ({0}) {1}", i.ToString(CultureInfo.CurrentCulture.NumberFormat).PadLeft(3), SupportCommon.DateTimeIdentifier); var rhyMaterial = RandomRhythmicMaterial(name, rhyStructs, numberOfStructs); //// rhythmicOrder, coll.Add(rhyMaterial); } return coll; } #endregion #region Public methods /// Makes a deep copy of the object. /// Returns object. [UsedImplicitly] public object Clone() { var def = new RhythmicMaterial { Name = this.Name }; return def; } #endregion #region String representation /// String representation of the object. /// Returns value. public override string ToString() { var s = new StringBuilder(); ////s.Append("\t" + this.HarmonicOrder.ToString(CultureInfo.CurrentCulture)); return s.ToString(); } #endregion #region Rhythmic producer - private /// /// Random Rhythmic Material. /// /// Name of material. /// Rhythmic Structs. /// Number Of Structs. /// /// Returns value. /// private static RhythmicMaterial RandomRhythmicMaterial(string name, Collection rhythmicStructs, int numberOfStructs) { //// byte rhythmicOrder, Contract.Requires(name != null); if (rhythmicStructs == null) { return null; } var rhythmicMaterial = new RhythmicMaterial { Name = name }; for (var im = 0; im < numberOfStructs; im++) { var rs = ExtendCollection.GetRandomObject(rhythmicStructs); if (rs == null) { continue; } var mrs = rs; //// Clone? rhythmicMaterial.Structures?.Add(mrs); } return rhythmicMaterial; } #endregion } }